using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;

namespace PackTracker.Business
{ 
	[Serializable()] 
	public class ClienteCollection : Csla.BusinessListBase<ClienteCollection, Cliente>
	{
		#region BindingList Overrides
		protected override object AddNewCore()
		{
			Cliente item = Cliente.NewCliente();
			this.Add(item);
			return item;
		}
		#endregion //BindingList Overrides

		#region Authorization Rules

		public static bool CanGetObject()
		{
			//TODO: Define CanGetObject permission in ClienteCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ClienteCollectionViewGroup"))
			//	return true;
			//return false;
		}

		public static bool CanAddObject()
		{
			//TODO: Define CanAddObject permission in ClienteCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ClienteCollectionAddGroup"))
			//	return true;
			//return false;
		}

		public static bool CanEditObject()
		{
			//TODO: Define CanEditObject permission in ClienteCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ClienteCollectionEditGroup"))
			//	return true;
			//return false;
		}

		public static bool CanDeleteObject()
		{
			//TODO: Define CanDeleteObject permission in ClienteCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ClienteCollectionDeleteGroup"))
			//	return true;
			//return false;
		}
		#endregion //Authorization Rules

		#region Factory Methods
		private ClienteCollection()
		{ /* require use of factory method */ }

		private ClienteCollection(SafeDataReader dr)
		{
			MarkAsChild();
			Fetch(dr);
		}

		public static ClienteCollection NewClienteCollection()
		{
			if (!CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a ClienteCollection");
			return new ClienteCollection();
		}

		public static ClienteCollection GetClienteCollection()
		{
			if (!CanGetObject())
				throw new System.Security.SecurityException("User not authorized to view a ClienteCollection");
			return DataPortal.Fetch<ClienteCollection>(new FilterCriteria());
		}

		internal static ClienteCollection GetClienteCollection(SafeDataReader dr)
		{
			return new ClienteCollection(dr);
		}

		/*
			public static ClienteCollection GetClienteCollection(int idEndereco, int idUsuario)
			{
				if (!CanGetObject())
					throw new System.Security.SecurityException("User not authorized to view a ClienteCollection");
				return DataPortal.Fetch<ClienteCollection>(new FilterCriteria(idEndereco, idUsuario));
			}
		*/

		#endregion //Factory Methods

		#region Data Access

		#region Filter Criteria
		[Serializable()]
		private class FilterCriteria
		{
			public int IdEndereco;
			public int IdUsuario;

			public FilterCriteria()
			{
			}

			public FilterCriteria(int idEndereco, int idUsuario)
			{
				this.IdEndereco = idEndereco;
				this.IdUsuario = idUsuario;
			}
		}
		#endregion //Filter Criteria

		#region Data Access - Fetch
		private void DataPortal_Fetch(FilterCriteria criteria)
		{
			RaiseListChangedEvents = false;

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteFetch(cn, criteria);
			}//using

			RaiseListChangedEvents = true;
		}

		private void ExecuteFetch(SqlConnection cn, FilterCriteria criteria)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_GetClienteCollection";
				/*
				cm.Parameters.AddWithValue("@idEndereco", criteria.IdEndereco);
				cm.Parameters.AddWithValue("@idUsuario", criteria.IdUsuario);
				*/

				using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
				{
					Fetch (dr);
				}
			}//using
		}

		private void Fetch(SafeDataReader dr)
		{
			while (dr.Read())
				this.Add(Cliente.GetCliente(dr));
		}

		#endregion //Data Access - Fetch

		#region Data Access - Update
		protected override void DataPortal_Update()
		{

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				Update(cn);

			}//using SqlConnection
		}

		internal void Update(SqlConnection cn)
		{
			RaiseListChangedEvents = false;

			// loop through each deleted child object
			foreach (Cliente deletedChild in DeletedList)
				deletedChild.DeleteSelf(cn);
			DeletedList.Clear();

			// loop through each non-deleted child object
			foreach (Cliente child in this)
			{
				if (child.IsNew)
					child.Insert(cn);
				else
					child.Update(cn);
			}

			RaiseListChangedEvents = true;
		}

		#endregion //Data Access - Update
		#endregion //Data Access
	}
}
